package controller

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/gin-gonic/gin"
	"net/http"
)

type JsonSuccessStruct struct {
	Code    int         `json:"code"`
	Message interface{} `json:"message"`
	Data    interface{} `json:"data"`
	Count   int64       `json:"count"`
}

type JsonErrorStruct struct {
	Code    int         `json:"code"`
	Message interface{} `json:"message"`
}

func ReturnSuccess(c *gin.Context, code int, message interface{}, data interface{}, count int64) {
	json := &JsonSuccessStruct{Code: code, Message: message, Data: data, Count: count}
	c.JSON(http.StatusOK, json)
}

func ReturnError(c *gin.Context, code int, message interface{}) {
	json := &JsonErrorStruct{Code: code, Message: message}
	c.JSON(http.StatusOK, json)
}

func EncryMd5(s string) string {
	ctx := md5.New()
	ctx.Write([]byte(s))
	return hex.EncodeToString(ctx.Sum(nil))
}
